﻿Public Class clsAccount
    Private strMsg As String = ""

    Public ReadOnly Property SetMsg() As String
        Get
            Return strMsg
        End Get
    End Property

    Public Function chkAccountDisposit(ByVal strAcc As String) As Boolean
        Dim blnChk As Boolean = True
        Dim strAccDel As String = ""
        Dim strCutAcc(11) As String

        Try

            If Not strAcc = "" Then
                strAccDel = strAcc.Replace("-", "").Replace(" ", "")
                If strAccDel.Length = 12 Then
                    Try
                        Dim ChkNumber As Double = strAccDel

                    Catch ex As Exception
                        strMsg = "ข้อมูลเลขที่บัญชีไม่ครบ 12 หลัก"
                        Throw ex
                        Exit Function
                    End Try
                    Try
                        For i As Integer = 0 To strCutAcc.Length - 1
                            strCutAcc(i) = strAccDel.Substring(i, 1)
                        Next
                        Dim intLastNumAcc As Integer = calculateDisposit(strCutAcc)

                        If intLastNumAcc = CInt(strCutAcc(11)) Then
                            blnChk = True
                        Else
                            strMsg = "เลขที่บัญชีผิด กรุณาตรวจสอบเลขที่บัญชี"
                            blnChk = False
                        End If
                    Catch ex As Exception
                        strMsg = "เกิดข้อผิดพลาดในระหว่างการตรวจสอบ"
                        Throw ex
                        blnChk = False
                        Exit Function
                    End Try
                Else
                    strMsg = "ข้อมูลเลขที่บัญชีไม่ครบ 12 หลัก"
                    blnChk = False
                End If
            Else
                strMsg = "กรุณากรอกข้อมูลเลขที่บัญชี"
                blnChk = False
            End If
        Catch ex As Exception
            strMsg = "เกิดข้อมูลพลาดในการตรวจสอบ เลขที่บัญชี"
            blnChk = False
            Throw ex
        End Try
        Return blnChk
    End Function

    Public Function chkAccountLoan(ByVal strAcc As String) As Boolean
        Dim blnChk As Boolean = True
        Dim strAccDel As String = ""
        Dim strCutAcc(11) As String

        Try

            If Not strAcc = "" Then
                strAccDel = strAcc.Replace("-", "").Replace(" ", "")
                If strAccDel.Length = 12 Then
                    Try
                        Dim ChkNumber As Double = strAccDel

                    Catch ex As Exception
                        strMsg = "ข้อมูลเลขที่บัญชีไม่ครบ 12 หลัก"
                        Throw ex
                        Exit Function
                    End Try
                    Try
                        For i As Integer = 0 To strCutAcc.Length - 1
                            strCutAcc(i) = strAccDel.Substring(i, 1)
                        Next
                        Dim intLastNumAcc As Integer = calculateLoan(strCutAcc)

                        If intLastNumAcc = CInt(strCutAcc(11)) Then
                            blnChk = True
                        Else
                            strMsg = "เลขที่บัญชีผิด กรุณาตรวจสอบเลขที่บัญชี"
                            blnChk = False
                        End If
                    Catch ex As Exception
                        strMsg = "เกิดข้อผิดพลาดในระหว่างการตรวจสอบ"
                        Throw ex
                        blnChk = False
                        Exit Function
                    End Try
                Else
                    strMsg = "ข้อมูลเลขที่บัญชีไม่ครบ 12 หลัก"
                    blnChk = False
                End If
            Else
                strMsg = "กรุณากรอกข้อมูลเลขที่บัญชี"
                blnChk = False
            End If
        Catch ex As Exception
            strMsg = "เกิดข้อมูลพลาดในการตรวจสอบ เลขที่บัญชี"
            blnChk = False
            Throw ex
        End Try
        Return blnChk
    End Function

    Private Function calculateDisposit(ByVal acc() As String) As Integer
        Dim accLast As Integer = 0
        Dim intSumCal As Integer = 0

        For i As Integer = 0 To acc.Length - 2
            Select Case i
                Case 0
                    intSumCal += acc(i) * 5
                Case 1
                    intSumCal += acc(i) * 4
                Case 2
                    intSumCal += acc(i) * 3
                Case 3
                    intSumCal += acc(i) * 2
                Case 4
                    intSumCal += acc(i) * 7
                Case 5
                    intSumCal += acc(i) * 6
                Case 6
                    intSumCal += acc(i) * 6
                Case 7
                    intSumCal += acc(i) * 5
                Case 8
                    intSumCal += acc(i) * 4
                Case 9
                    intSumCal += acc(i) * 3
                Case 10
                    intSumCal += acc(i) * 2
            End Select
        Next
        ' accLast = CInt(10 - (intSumCal Mod 10))
        If (intSumCal Mod 10) = 0 Then
            accLast = 0
        Else
            accLast = CInt(10 - (intSumCal Mod 10))
        End If

        Return accLast
    End Function

    Private Function calculateLoan(ByVal acc() As String) As Integer
        Dim accLast As Integer = 0
        Dim intSumCal As Integer = 0

        For i As Integer = 0 To acc.Length - 2
            Select Case i
                Case 0
                    intSumCal += acc(i) * 6
                Case 1
                    intSumCal += acc(i) * 5
                Case 2
                    intSumCal += acc(i) * 4
                Case 3
                    intSumCal += acc(i) * 3
                Case 4
                    intSumCal += acc(i) * 2
                Case 5
                    intSumCal += acc(i) * 7
                Case 6
                    intSumCal += acc(i) * 6
                Case 7
                    intSumCal += acc(i) * 5
                Case 8
                    intSumCal += acc(i) * 4
                Case 9
                    intSumCal += acc(i) * 3
                Case 10
                    intSumCal += acc(i) * 2
            End Select
        Next
        If (intSumCal Mod 10) = 0 Then
            accLast = 0
        Else
            accLast = CInt(10 - (intSumCal Mod 10))
        End If

        Return accLast
    End Function

End Class
